package ar.com.gals.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ar.com.gals.error.GalsException;
import ar.com.gals.service.LocalidadService;

@Controller
@RequestMapping(value = "/Ubicacion")
public class UbicacionController extends AbstractController {

	@RequestMapping(value = "/Localidad", method = RequestMethod.POST)
    public @ResponseBody String obtenerLocalidades(HttpServletRequest request, HttpServletResponse response) {
	
		try
		{
			int provinciaId = Integer.parseInt(request.getParameter("provinciaId"));
			
			JSONObject json = new JSONObject();
			json.put("localidades", new LocalidadService().getByProvincia(provinciaId));
			json.put("error", "");
			return json.toString();
		}
		catch(GalsException ex) {
			JSONObject json = new JSONObject();
			json.put("error", ex.getErrors().get(0).getDescripcion());
			return json.toString();
		}
		catch(Exception ex2) {
			JSONObject json = new JSONObject();
			json.put("error", ex2.getMessage());
			return json.toString();
		}
	}
}
